package com.website.main.util;

import com.website.common.vo.Tree;
import com.website.mybatis.entity.SysPower;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

public class Utils {

	public static List<Tree> powerTOTreeMenu(List<SysPower> litt){
		return powerTOTreeMenu(litt, null);
	}

	public static List<Tree> powerTOTreeMenu(List<SysPower> litt, Boolean checked){
		List<Tree> res = new ArrayList<>();
		for(SysPower p: litt){
			Tree tree = new Tree();
			if(p.getParentId() == null){
				BeanUtils.copyProperties(p, tree);
				tree.setText(p.getName());
				tree.setHref_(p.getUrl());
				tree.setExpanded(p.getExpand());
				tree.setParentId(null);
				if(Boolean.TRUE.equals(checked)){
					tree.setChecked(false);
				}
				initMenu(litt, tree, checked);
				res.add(tree);
			}
		}
		return res;
	}

	private static void initMenu(List<SysPower> litt, Tree root, Boolean checked) {
		for(SysPower p: litt){
			if(p.getParentId() !=null && p.getParentId().equals(root.getId())){
				Tree tree = new Tree();
				BeanUtils.copyProperties(p, tree);
				tree.setText(p.getName());
				tree.setHref_(p.getUrl());
				tree.setExpanded(p.getExpand());
				root.addChild(tree);
				if(Boolean.TRUE.equals(checked)){
					tree.setChecked(false);
				}
				if(!tree.isLeaf()){
					initMenu(litt, tree, checked);
				}
			}
		}
	}
}
